package com.agrm.cart.service.impl;

import com.agrm.api.client.GoodClient;
import com.agrm.api.client.MemberClient;
import com.agrm.cart.mapper.CartMapper;
import com.agrm.cart.service.CartService;
import com.agrm.model.cart.pojo.Cart;
import com.agrm.model.cart.pojo.CartGoods;
import com.agrm.model.cart.pojo.CartItem;
import com.agrm.model.common.dtos.ResponseResult;
import com.agrm.model.common.enums.AppHttpCodeEnum;
import com.agrm.model.good.pojo.Good;
import com.agrm.model.member.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {


    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private MemberClient memberClient;

    @Autowired
    private GoodClient goodClient;

    /**
     * 获取用户的购物车列表
     * @param memberId 用户ID
     * @return
     */
    @Override
    public List<Cart> getAllCarts(Integer memberId) {
        // 根据id查询用户
        ResponseResult<Member> memberResponseResult = memberClient.getById(memberId);
        Member member = memberResponseResult.getData();
        if(member == null){
            throw new RuntimeException("用户不存在");
        }

        //查询购物车信息
        List<Cart> carts = cartMapper.getCartsByMemberId(memberId);

        if(carts != null){
            //设置商品信息
            for (Cart cart : carts) {
                ResponseResult<Good> goodResponseResult = goodClient.getDetailById(cart.getGoodId());
                cart.setGood(goodResponseResult.getData());
            }
        }
        return carts;
    }


    /**
     * 添加购物车
     * @param cart 购物车
     * @return 影响的记录数
     */
    @Override
    public ResponseResult addCart(Cart cart) {
        if(cart.getId() == null){
            //第一次添加
            cart.setQty(1);
            try {
                return ResponseResult.okResult( 2 , "操作成功" , cartMapper.insert(cart));
            } catch (Exception e) {
                return ResponseResult.okResult(1 ,"不要重复添加");
            }
        }

        // 不是第一次添加 查询购物车信息
        Cart cartData = cartMapper.selectById(cart.getId());
        if(cartData == null){
            throw new RuntimeException("购物车数据不存在");
        }
        ResponseResult<Good> GoodResponseResult = goodClient.getById(cartData.getGoodId());
        Good good = GoodResponseResult.getData();
        if(good.getQty() < cart.getQty()){
            throw new RuntimeException("商品库存不足");
        }

        cartData.setQty(cart.getQty());

        return ResponseResult.okResult( 2 , "操作成功" , cartMapper.updateById(cartData));
    }


    /**
     * 删除购物车
     * @param ids ids
     * @return
     */
    @Override
    public int deleteCartByIds(List<Integer> ids) {
        if(ids == null){
            throw new RuntimeException("参数错误");
        }
        return cartMapper.deleteCartByIds(ids);
    }


    /**
     * 获取购物车商品
     * @param ids 购物车ids
     * @return 购物车商品信息
     */
    @Override
    public CartGoods listGoodByCart(List<Integer> ids) {
        //1.把购物车记录查出来
        List<Cart> carts = cartMapper.selectByIds(ids);
        if (carts == null){
            throw new RuntimeException("购物车数据不存在");
        }

        //2.循环购物车记录 获取商品信息
        List<CartItem> goods = new ArrayList<>();
        BigDecimal marketPrice = new BigDecimal(0);
        BigDecimal price = new BigDecimal(0);
        for (Cart cart : carts) {
            ResponseResult<Good> goodResponseResult = goodClient.getById(cart.getGoodId()); // 获取商品信息
            Good good = goodResponseResult.getData();

            CartItem cartItem = new CartItem(good , cart.getQty() , cart.getId());
            goods.add(cartItem); // 商品信息 和 数量
            marketPrice = marketPrice.add(good.getMarkPrice().multiply(new BigDecimal(cart.getQty())));
            price = price.add(good.getPrice().multiply(new BigDecimal(cart.getQty())));
        }

        //3.封装数据
        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoods(goods);
        cartGoods.setMarketPrice(marketPrice);
        cartGoods.setPrice(price);
        return cartGoods;
    }

}
